from django.core.cache import cache
from luffy.settings.dev import BONNER_COUNT
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response

from .models import BannerModel
from .serializer import BannerSerializer

# Create your views here.


class BannerViewSet(ModelViewSet):
    queryset = BannerModel.objects.filter(
        is_delete=False, is_show=True).order_by("orders")[:BONNER_COUNT]
    serializer_class = BannerSerializer

    def list(self, request, *args, **kwargs):
        banner_list = cache.get("banner_list")
        if not banner_list:
            response =  super().list(request, *args, **kwargs)
            cache.set("banner_list", response.data, 60*60*24)
            return response
        return Response(data=banner_list)
